/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.upc.sad.dao;

import java.util.List;
import java.util.ArrayList;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import pe.edu.upc.sad.model.Empresa;
import pe.edu.upc.sad.util.BError;
import pe.edu.upc.sad.util.Conexion;
import pe.edu.upc.sad.util.ToolSql;
/**
 *
 * @author aalanya
 */
public class EmpresaDaoImpl implements EmpresaDao {

    private Statement  st   = null;
    private ResultSet  rs   = null;
    private CallableStatement stmt =null;
    private StringBuffer query = null;
    Connection conn = null;
    private BError error;

    public EmpresaDaoImpl()
    {
     
    }

    public void Inicio()
    {
       error = new BError();
       query = new StringBuffer("");
       conn =   Conexion.getConnection();
    }

    public Empresa get(String ruc, String clave) throws Exception
    {
         Empresa company = null;
         try{
 Inicio();

            query.append("{ call USP_SELECT_EMPRESA( ?, ? ) }");
            stmt = conn.prepareCall(query.toString());
            stmt.setString( 1, ruc.toString().substring(0, 11) );
            stmt.setString( 2, clave );
            stmt.execute();
            rs = stmt.getResultSet();
            while (rs.next())
            {
              company = new Empresa();
              company.setRuc(rs.getString("Ruc"));
              company.setRazonSocial(rs.getString("RazonSocial"));
              company.setDireccion(rs.getString("Direccion"));
              company.setContacto(rs.getString("Contacto"));
              company.setEmail(rs.getString("Email"));
              company.setNroCuenta(rs.getString("NroCuenta"));
 
            }
            return company;
        }catch (Exception e){
            error.setCodError(100);
            error.setMsgError("Error :" + e.getMessage());
            return null;
        }finally{
             ToolSql.close(rs, st, stmt, conn);
        }
        
   }

  public int insert (Empresa company ) throws SQLException
  {
   int resultado = 0;
   try
    {
         Inicio();

      query.append("{ call USP_INSERT_EMPRESA( ?, ?, ?, ?, ?, ?, ? ) }");

      stmt = conn.prepareCall(query.toString());
      stmt.setString(1,company.getRuc());
      stmt.setString(2,company.getRazonSocial());
      stmt.setString(3,company.getDireccion());
      stmt.setString(4,company.getClave());
      stmt.setString(5,company.getContacto());
      stmt.setString(6,company.getEmail());
      stmt.setString(7,company.getNroCuenta());
      stmt.execute();
      resultado = 1;//stmt.getInt(32);

    }
    catch(Exception e)
    {
        error.setCodError(100);
        error.setMsgError("Error :" + e.getMessage());
    }
    finally
    {
       ToolSql.close(rs, st, stmt, conn);
    }
    return resultado;
  }

  public List<Empresa> getListcompany(Connection conn) throws Exception
  {

     List lstCompany = new ArrayList<Empresa>();

    try
    {
 Inicio();

      stmt = conn.prepareCall( "call USP_LISTAR_EMPRESA" );
      
      rs = stmt.executeQuery();

        while (rs.next())
        {

          Empresa company = new Empresa();

          company.setRuc(rs.getString("Ruc"));
          company.setRazonSocial(rs.getString("RazonSocial"));
          company.setDireccion(rs.getString("Direccion"));
          company.setEmail(rs.getString("Email"));
          company.setNroCuenta(rs.getString("NroCuenta"));

          lstCompany.add(company);

        }

    }catch(Exception e)
    {
        error.setCodError(100);
        error.setMsgError("Error :" + e.getMessage());
    }
    finally
    {
       ToolSql.close(rs, st, stmt, conn);
    }
    return  lstCompany;
  }

  
}
